package com.helen.db;
import android.content.Context;
import com.helen.entity.LoginRegisterEntity;
import com.helen.global.Constants;
import com.helen.utils.GsonUtils;
import com.helen.utils.MD5;
import com.helen.utils.SharedPreferencesUtils;
import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * Created by wangyang on 2017/3/23 0030.
 */
public class UserHelper {
    private static UserHelper mInstance = null;
    private DbManager db = null;
    private String userAccount = null;
    private String userPassword = null;
    private String AUTH_KEY = null;         //MD5(UserName+APPName+UserPassword)
    private UserTable mUser = null;
    private LoginRegisterEntity loginEntity = null;
    private boolean isLogined = false;
    public static synchronized UserHelper getInstance() {
        if (mInstance == null) {
            mInstance = new UserHelper();
        }
        return mInstance;
    }

    public boolean isLogined() {
        return isLogined;
    }

    public void setIsLogined(boolean b) {
        this.isLogined = b;
    }

    public void saveUserAuth(String userName, String userPassword) {
        saveUserAuth(userName, userPassword, false);
    }

    public void saveUserAuth(String userName, String userPassword, boolean isGenerateAuthKey) {
        if (db == null) {
            db = DbHelper.getInstance().getDb();
        }
        try {
            UserTable tmpUser = db.selector(UserTable.class).where("useraccount", "=", userName).findFirst();

            if (tmpUser == null) {
                UserTable user = new UserTable();
                user.setUserAccount(userName);
                user.setUserPassword(userPassword);
                user.setLastLoginTime(System.currentTimeMillis());
                db.saveOrUpdate(user);
            } else {
                tmpUser.setUserAccount(userName);
                tmpUser.setUserPassword(userPassword);
                tmpUser.setLastLoginTime(System.currentTimeMillis());
                db.update(tmpUser);
            }

        } catch (DbException e) {
            e.printStackTrace();
        }
        if (isGenerateAuthKey) {
            generateAuthKey(userName, userPassword);

        }
    }

    public UserTable getLastLoginUser() {
        if (db == null) {
            db = DbHelper.getInstance().getDb();
        }
        try {
            mUser = db.selector(UserTable.class).where("lastlogintime", ">", System.currentTimeMillis() - 7 * 24 * 3600 * 1000).orderBy("lastlogintime", true).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        if (mUser != null) {
            userAccount = mUser.getUserAccount();
            userPassword = mUser.getUserPassword();
        }
        return mUser;
    }

    public void generateAuthKey(String userName, String userPassword) {
        this.userAccount = userName;
        this.userPassword = userPassword;
        AUTH_KEY = MD5.getMD5Str(userName + Constants.APP_PREF + userPassword);
    }





    public String getUserAccount() {
        if (userAccount == null) {
            getLastLoginUser();
        }
        return userAccount;
    }

    public void setUserAccount(String userAccount) {
        this.userAccount = userAccount;
    }

    public String getUserPassword() {
        if (userPassword == null) {
            getLastLoginUser();
        }
        return userPassword;
    }


    public LoginRegisterEntity getloginEntity(Context mContext) {
        if (loginEntity == null) {
            if (AUTH_KEY == null) {
                getLastLoginUser();
                if (mUser == null) {
                    return null;
                }
                generateAuthKey(mUser.getUserAccount(), mUser.getUserPassword());
            }
            if (AUTH_KEY == null || !AUTH_KEY.equals(getAuthKeyFromStorage(mContext))) {
                return null;
            } else {
                loginEntity = GsonUtils.getBean((String) SharedPreferencesUtils.getParam(mContext, AUTH_KEY + Constants.SHARED_PREFERENCES_KEY_LOGIN_MODEL, ""), LoginRegisterEntity.class);
            }
        }

        return loginEntity;
    }
    public void setloginEntity(Context mContext, LoginRegisterEntity loginEntity) {
        this.loginEntity = loginEntity;
        SharedPreferencesUtils.setParam(mContext, AUTH_KEY + Constants.SHARED_PREFERENCES_KEY_LOGIN_MODEL, GsonUtils.GsonToString(loginEntity));
    }


    public void saveUserInfo(Context mContext, String entity) {
        SharedPreferencesUtils.setParam(mContext, userAccount + Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_STORAGE, AUTH_KEY);
        SharedPreferencesUtils.setParam(mContext, AUTH_KEY + Constants.SHARED_PREFERENCES_KEY_LOGIN_MODEL, entity);
        loginEntity = GsonUtils.getBean(entity, LoginRegisterEntity.class);


    }

    public String getAuthKeyFromStorage(Context mContext) {
        return (String) SharedPreferencesUtils.getParam(mContext, userAccount + Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_STORAGE, "");
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getAUTH_KEY() {
        return AUTH_KEY;
    }

    public void setAUTH_KEY(String AUTH_KEY) {
        this.AUTH_KEY = AUTH_KEY;
    }
}
